package com.swz.dcrm.adpter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.swz.dcrm.R;
import com.swz.dcrm.base.ViewHolder;
import com.swz.dcrm.model.StaffGoal;
import com.swz.dcrm.util.Tool;


import java.util.List;

public class EditStaffGoalAdapter extends CustomAdapter<StaffGoal> {


    private TextChangeListener textChangeListener;
    public interface TextChangeListener{
        void onChange(List<StaffGoal>list);
    }

    public EditStaffGoalAdapter(Context context, List<StaffGoal> datas) {
        super(context, R.layout.item_edit_staff_goal, datas);
    }

    @Override
    protected void convert(ViewHolder holder, StaffGoal staffGoal, int position) {
        holder.setText(R.id.tv_name,staffGoal.getMemeberName());

        EditText goal1=holder.getView(R.id.et_goal1);
        EditText goal2=holder.getView(R.id.et_goal2);
        EditText goal3=holder.getView(R.id.et_goal3);

        goal1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(goal1.getText().toString())){
                    staffGoal.setBuildFileCount(Integer.valueOf(0));
                }else {
                    staffGoal.setBuildFileCount(Integer.valueOf(goal1.getText().toString()));
                }

               if(textChangeListener!=null){
                   textChangeListener.onChange(mDatas);
               }
            }
        });
        goal2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(goal2.getText().toString())){
                    staffGoal.setOrderCarCount(0);
                }else {
                    staffGoal.setOrderCarCount(Integer.valueOf(goal2.getText().toString()));
                }

                if(textChangeListener!=null){
                    textChangeListener.onChange(mDatas);
                }
            }
        });
        goal3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(goal3.getText().toString())){
                    staffGoal.setDeliveryCarCount(0);
                }else {
                    staffGoal.setDeliveryCarCount(Integer.valueOf(goal3.getText().toString()));
                }

                if(textChangeListener!=null){
                    textChangeListener.onChange(mDatas);
                }
            }
        });

        holder.setText(R.id.et_goal1,staffGoal.getBuildFileCount()+"");
        holder.setText(R.id.et_goal2,staffGoal.getOrderCarCount()+"");
        holder.setText(R.id.et_goal3,staffGoal.getDeliveryCarCount()+"");

    }

    public void setTextChangeListener(TextChangeListener textChangeListener) {
        this.textChangeListener = textChangeListener;
    }
}
